package com.twt.sys.base;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twt.common.utils.Assert;
import com.twt.model.base.BaseEntity;
import com.twt.model.system.entity.SysDictItem;
import com.twt.sys.mapper.SysDictItemMapper;

import java.util.Arrays;

/**
 * @Desciption:
 * @ClassName:AbstractBaseService
 * @Author:TwT
 * @Date:2022/6/19 1:16
 * @Version:1.0
 **/
public abstract class AbstractBaseService<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
    @Override
    public void add(T addVo) {
        //保存前方法
        this.beforeSave(addVo);

        //插入数据库
        addVo.setId(IdUtil.getSnowflakeNextIdStr());
        this.save(addVo);
    }

    @Override
    public void deleteByIds(String ids) {
        Assert.isBlank(ids, "ids不能为空");
        this.removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public void editById(T editVo) {
        Assert.isBlank(editVo.getId(), "id 不能为空！");

        this.beforeSave(editVo);

        this.updateById(editVo);
    }

    @Override
    public T queryOneById(String id) {
        Assert.isBlank(id, "查询主键不能为空");
        return this.getById(id);
    }

    protected abstract void beforeSave(T po);

}
